<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { ref, reactive } from 'vue'
import { AddressParams } from '@/types/address'
import { updateAddress, addAddress, getAddressById } from '@/api/address'
import { Rules, validate } from '@/utils/validate'

const form = reactive({
  isDefault: 0
} as AddressParams)

let addressId = ref('')
onLoad(async ({ id }) => {
  uni.setNavigationBarTitle({
    title: id ? '修改地址' : '新建地址'
  })

  // 修改
  if (id) {
    addressId.value = id
    // 根据id查询要修改的地址信息
    const res = await getAddressById(id)
    Object.assign(form, res)
    fullLocation.value = res.fullLocation
  }
})

// 省市区发生了改变
const fullLocation = ref('')
const regionChange = (e: WechatMiniprogram.PickerChange) => {
  form.provinceCode = e.detail.code[0]
  form.cityCode = e.detail.code[1]
  form.countyCode = e.detail.code[2]

  fullLocation.value = (e.detail.value as string[]).join('')
}

// 默认地址修改
const defaultChange = (e: any) => {
  form.isDefault = e.detail.value ? 1 : 0
}

// 自定义校验规则
const rules: Rules = {
  receiver: [{ required: true, message: '请填写收货人姓名' }],
  contact: [
    { required: true, message: '请填写收货人手机号码' },
    { pattern: /^1\d{10}$/, message: '请填写正确的手机号码' }
  ],
  provinceCode: [{ required: true, message: '请选择收货人所在城市' }],
  address: [{ required: true, message: '请填写详细收货地址' }]
}

// 提交
const submit = async () => {
  const valid = await validate(form, rules)
  if (!valid) return
  if (addressId.value) {
    await updateAddress(addressId.value, form)
  } else {
    await addAddress(form)
  }

  // 提示
  uni.showToast({
    title: addressId ? '修改成功~' : '新增成功~',
    icon: 'none'
  })

  // 返回到上一个页面
  uni.navigateBack({})
}
</script>

<template>
  <view class="viewport">
    <!-- 地址信息 -->
    <view class="form">
      <view class="form-item">
        <text class="label">姓名</text>
        <input
          v-model="form.receiver"
          placeholder-style="color: #888"
          placeholder="请填写收货人姓名"
        />
      </view>
      <view class="form-item">
        <text class="label">手机号码</text>
        <input
          v-model="form.contact"
          placeholder-style="color: #888"
          placeholder="请填写收货人手机号码"
        />
      </view>
      <view class="form-item">
        <text class="label">省/市/县 (区)</text>
        <picker mode="region" @change="regionChange">
          <view v-if="fullLocation" class="region">{{ fullLocation }}</view>
          <view v-else class="placeholder">请填写收货人所在城市</view>
        </picker>
      </view>
      <view class="form-item">
        <text class="label">详细地址</text>
        <input
          v-model="form.address"
          placeholder-style="color: #888"
          placeholder="街道、楼牌号信息"
        />
      </view>
      <view class="form-item">
        <text class="label">设置默认地址</text>
        <switch
          color="#27ba9b"
          :checked="form.isDefault"
          @change="defaultChange"
        />
      </view>
    </view>
    <!-- 提交按钮 -->
    <view class="button" @click="submit">保 存</view>
  </view>
</template>

<style>
page {
  background-color: #f4f4f4;
}

.form {
  margin: 20rpx 20rpx 0;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;
}

.form .form-item {
  display: flex;
  min-height: 96rpx;
  line-height: 46rpx;
  padding: 25rpx 10rpx;
  background-color: #fff;
  font-size: 28rpx;
  border-bottom: 1rpx solid #ddd;
  position: relative;
}

.form .form-item:last-child {
  border: none;
}

.form .form-item .label {
  width: 200rpx;
  color: #333;
}

.form .form-item input {
  flex: 1;
  display: block;
  height: 46rpx;
}

.form .form-item switch {
  position: absolute;
  right: -10rpx;
  transform: scale(0.7) translateY(-8px);
}

.form .form-item picker {
  flex: 1;
}

.form .form-item .region {
  color: #333;
}

.form .form-item .placeholder {
  color: #888;
}

.button {
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
  margin: 30rpx 20rpx;
  color: #fff;
  border-radius: 80rpx;
  font-size: 30rpx;
  background-color: #27ba9b;
}
</style>
